package com.xiguaji.tiktok.task.aweme;

import com.xiguaji.boot.data.rdb.aggregate.DistributedAggregate;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xiguaji.boot.toolkit.MapperUtils;
import com.xiguaji.tiktok.task.aweme.spider.model.Aweme_list;
import com.xiguaji.tiktok.task.aweme.spider.model.Text_extra;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Aweme extends DistributedAggregate {
    /**
     * 作品Id
     */
    private long oAwemeId;
    /**
     * 作品说明
     */
    private String oDesc;
    /**
     * 创建时间
     */
    private long oCreateTime;
    /**
     * 用户id
     */
    private long oUid;
    /**
     * 音乐id
     */
    private long oMusicId;
    /**
     * 评论数
     */
    private int oCommentCount;
    /**
     * 点击数
     */
    private long oDiggCount;
    /**
     * 下载数
     */
    private int oDownloadCount;
    /**
     * 播放数
     */
    private long oPlayCount;
    /**
     * 分享数
     */
    private int oShareCount;
    /**
     * Whatsapp 分享数量
     */
    private int oWhatsappShareCount;
    /**
     * 评分
     */
    private int oRate;
    /**
     * 标签
     */
    private String oTextExtra;
    /**
     * 分享链接
     */
    private String oShareUrl;
    /**
     * 视频长度 秒
     */
    private int oDuration;
    /**
     * 区域
     */
    private String oRegion;
    /**
     * 是否广告
     */
    private boolean oIsAds;

    private int oForwardCount;

    private String oBloggerNickName;

    private String oBloggerUniqueId;

    @Mapper(componentModel = "spring", imports = {DateTimeUtils.class, MapperUtils.class})
    public interface AwemeMapper {
        List<Aweme> mapper(List<Aweme_list> list);

        @Mapping(source = "aweme_id", target = "oAwemeId")
        @Mapping(source = "desc", target = "oDesc")
        @Mapping(source = "create_time", target = "oCreateTime")
        @Mapping(source = "author.uid", target = "oUid")
        @Mapping(source = "music.id", target = "oMusicId")
        @Mapping(source = "statistics.comment_count", target = "oCommentCount")
        @Mapping(source = "statistics.digg_count", target = "oDiggCount")
        @Mapping(source = "statistics.download_count", target = "oDownloadCount")
        @Mapping(source = "statistics.play_count", target = "oPlayCount")
        @Mapping(source = "statistics.share_count", target = "oShareCount")
        @Mapping(source = "statistics.forward_count", target = "oForwardCount")
        @Mapping(source = "statistics.whatsapp_share_count", target = "oWhatsappShareCount")
        @Mapping(source = "rate", target = "oRate")
        @Mapping(target = "oTextExtra", expression = "java(text_extra(dto.getText_extra()))")
        @Mapping(source = "share_url", target = "oShareUrl")
        @Mapping(source = "duration", target = "oDuration")
        @Mapping(source = "region", target = "oRegion")
        @Mapping(source = "author.nickname", target = "oBloggerNickName")
        @Mapping(source = "author.unique_id", target = "oBloggerUniqueId")
        Aweme mapper(Aweme_list dto);

        default String text_extra(List<Text_extra> text_extras) {
            try {
                if (CollectionUtils.isEmpty(text_extras)) {
                    return "";
                }

                return MapperUtils.writeValue(
                        text_extras.stream().map(a -> {
                            Map<String, String> map = new HashMap<>();
                            map.put("name", a.getHashtag_name());
                            return map;
                        }).collect(Collectors.toList())
                );
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
